from django.http import HttpResponse, JsonResponse
from django.views.decorators.http import require_http_methods
import base64, json
from .confs.conf import get_mirror_conn
from django.views.decorators.csrf import csrf_exempt
from datetime import datetime

def_response_o = {
    "code": 0,
    "result": "",
    "message": "",
    "type": 'success',
  }


def get_data(params):
    now = datetime.now()
    print(params)
    if params:
        v = {
            'site': params["site"] if params["site"] else '',
            'ip': params["ip"] if params["ip"] else '',
            'create_time' : now
        }
        return v
    return ""

@require_http_methods(["POST"])
@csrf_exempt
def save_exclude_ip(request):
    params = json.loads(request.body)
    print(params)
    data=get_data(params)
    db_conn = get_mirror_conn()
    if data:
        db_conn.insert('dlz_exclude_ip', data)
    re = json.loads(json.dumps(def_response_o,ensure_ascii=False))
    return JsonResponse(re)


@require_http_methods(["GET"])
def get_exclude_ip_list(request):
    global select
    re = json.loads(json.dumps(def_response_o,ensure_ascii=False))
    db_conn = get_mirror_conn()
    sql="select * from dlz_exclude_ip"
    result=db_conn.query_all(sql)
    re['result'] = result
    return JsonResponse(re)